﻿using System;
using System.Collections.Generic;
using System.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Configuration;
using System.Data;
using System.Data.SqlClient;
using DAL;
using BLL;
using System.Text;


public partial class Plublic_Cards : System.Web.UI.Page
{
    string strConn = WebConfigurationManager.ConnectionStrings["conn"].ConnectionString.ToString();
    CardTypeHandler objCardTypeHandler = new CardTypeHandler();
    LocationHandler objLocationHandler = new LocationHandler();
    UsersHandler objUsersHandler = new UsersHandler();
    CardsHandler objCardsHandler = new CardsHandler();

    protected void Page_Load(object sender, EventArgs e)
    {
        if(!IsPostBack)
        {
            this.BindDropDowListCardType();
            this.BindDropDowListLocation();
            this.BindDropDowListAccHolders();
        }
    }

    protected void BindDropDowListAccHolders()
    {
        List<Users> lstUsers = new List<Users>();

        lstUsers = objUsersHandler.GetUsersList(strConn);

        ddlAccHolders.DataSource = lstUsers;
        ddlAccHolders.DataValueField = "UserID";
        ddlAccHolders.DataTextField = "FullName";
        ddlAccHolders.DataBind();
    }

    protected void BindDropDowListCardType()
    {
        List<CardType> lstCardType = new List<CardType>();

        lstCardType = objCardTypeHandler.GetCardTypeList(strConn);

        ddlCardType.DataSource = lstCardType;
        ddlCardType.DataValueField = "CardTypeID";
        ddlCardType.DataTextField = "Name";
        ddlCardType.DataBind();
    }

    protected void BindDropDowListLocation()
    {
        List<Location> lstLocation = new List<Location>();

        lstLocation = objLocationHandler.GetLocationList(strConn);

        ddlLocation.DataSource = lstLocation;
        ddlLocation.DataValueField = "LocationID";
        ddlLocation.DataTextField = "Address";
        ddlLocation.DataBind();
    }


    protected void calEventDate_SelectionChanged(object sender, EventArgs e)
    {
        txtOpenDate.Text = calEventDate.SelectedDate.ToString("MM/dd/yyyy");
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        Cards objCards = new Cards();

        objCards.Created = DateTime.Now.ToString();
        objCards.Updated = DateTime.Now.ToString();
        objCards.Isactive= "1";
        objCards.CardNumber = txtCardNumber.Text.ToString();
        objCards.AccountNumber = txtAccounNumber.Text.ToString();
        objCards.CardTypeID = Convert.ToInt32(ddlCardType.SelectedValue);
        objCards.UserID = Convert.ToInt32(ddlAccHolders.SelectedValue);
        objCards.Opendate = txtOpenDate.Text.ToString();
        objCards.Balance = Convert.ToDouble(txtBalance.Text);
        objCards.Currency = ddlCurrency.Text.ToString();
        objCards.LocationID = Convert.ToInt32(ddlLocation.SelectedValue);
         if(cboStatus.Checked ==true){
            objCards.Status="Draft";
        }else{
            objCards.Status="0";
        }
         objCards.Descriptions = txtDescriptions.Text.ToString();
         objCards.AccountHolders = ddlAccHolders.SelectedItem.ToString(); ;
         objCards.ExpirationDate = DateTime.Now.ToString();

         if (objCardsHandler.AddNewCards(objCards, strConn))
         {
             this.SetText();
         }
    }

    protected void SetText()
    {
        txtDescriptions.Text = "";
        txtAccounNumber.Text = "";
        txtBalance.Text = "";
        txtCardNumber.Text = "";
        txtOpenDate.Text = "";
        
    }
}